import os
import sys
import unittest

from limber.documents import basetextdocument
from tests.mocks import *

def setup_module():
    set_mockdir(2)


def testInstantiate():
    try:
        document = basetextdocument.BaseTextDocument()
    except Exception, e:
        assert False, 'Could not instantiate the BaseTextDocument: %r' % e


class TestBaseTextDocument(unittest.TestCase):
    def setUp(self):
        self.document = basetextdocument.BaseTextDocument()

    def testTitle(self):
        self.assertEqual(self.document.title(), None)
        self.document.set_title('Testing')
        self.assertEqual(self.document.title(), 'Testing')

    def testData(self):
        fname = os.path.join(get_userdir(), 'testData')
        content = '''
Testing the "data" method of the BaseTextDocument class.

This method should be able to retrieve data in a user-friendly way.
'''
        self.document.open(fname,'w+')
        self.document.write(content)

        query = basetextdocument.BaseTextDocumentQuery(from_pos=10, to_pos=35)
        got = self.document.data(query)
        self.document.close()

        try:
            os.remove(fname)
        except (IOError,OSError):
            print 'WARNING!\nCould not remove the file created in the test!'

        self.assertEqual(got,content[10:35])

        self.document.open(fname,'w+')
        self.document.write(content)

        query = basetextdocument.BaseTextDocumentQuery(from_pos=0, to_pos=-1)
        got = self.document.data(query)

        self.document.close()

        try:
            os.remove(fname)
        except (IOError,OSError):
            print 'WARNING!\nCould not remove the file created in the test!'

        self.assertEqual(got, content)

        self.document.open(fname,'w+')
        self.document.write(content)
        query = basetextdocument.BaseTextDocumentQuery(from_pos=0,to_pos=-1)
        format = str.splitlines

        got = self.document.data(query,format)

        self.document.close()

        try:
            os.remove(fname)
        except (IOError,OSError):
            print 'WARNING!\nCould not remove the file created in the test!'

        self.assertEqual(got, content.splitlines())

    def testSetData(self):
        fname = os.path.join(get_userdir(),'testSetData')
        content = '''
The "set_data" methods goes along with the "data" method.

It can perform specific operations of writing.
'''

        self.document.open(fname, 'w+')
        self.document.write(content)

        query = basetextdocument.BaseTextDocumentQuery(from_pos=10, to_pos=41)
        data_to_set = 'a'*31

        self.document.set_data(data_to_set, query)
        self.document.seek(0)

        new_content = self.document.read()

        self.document.close()

        try:
            os.remove(fname)
        except (IOError,OSError):
            print 'WARNING!\nCould not remove the file created in the test!'

        self.assertEqual(new_content, content[:10] + data_to_set + content[41:])
        
        self.document.open(fname, 'w+')
        self.document.write(content)
        
        query = basetextdocument.BaseTextDocumentQuery(from_pos=10, to_pos=41,
                                                       truncate=True)
        
        self.document.set_data(data_to_set, query)
        self.document.seek(0)
        
        new_content = self.document.read()
        self.document.close()
        
        try:
            os.remove(fname)
        except (IOError,OSError):
            print 'WARNING!\nCould not remove the file created in the test!'
        
        self.assertEqual(new_content, content[:10] + data_to_set + content[41])
